<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>部门管理</title>
	<link type="text/css" rel="stylesheet" href="../<s:property value="#session.themeStyle.mainCss" default="Skin/PM/LightBlue/main.css"/>" />
</head>
<body>
	<div class="navBar">
		当前位置：系统管理&gt;&gt;组织机构管理
	</div>
	<s:form theme="simple">
	<div class="tabBlock">
		<div style="margin-left: 5px;margin-bottom: 10px;margin-top: 10px;">
			<STRONG>当前组织机构：</STRONG><s:property value="page.findByExample.parent.name"/>&nbsp;&nbsp;
			<a class="but28" href="javascript:add();" style="text-decoration:none;">添加部门</a> 
			<a class="but28" href="userInfo-manage.action?deptId=<s:property value="page.findByExample.parent.pkId"/>" target="mainFrame" style="text-decoration:none;">管理用户</a> 
		</div>
		<div class="dataGrid" style="margin-top: 10px;">
			<table>
				<tr>
					<th style="width:5%">
						序号
					</th>
					<th style="width:10%">
						编号
					</th>
					<th style="width:20%">
						名称
					</th>
					<th style="">
						描述
					</th>
					<th style="width:15%">
						操作
					</th>
				</tr>
				<s:iterator value="page.data" status="st" >
					<tr class="trStyle">
						<td>
							<s:property value="#st.index+1+page.(pageNo-1)*page.pageSize"/>&nbsp;
						</td>
						<td>
							<s:property value="deptNo"/>&nbsp;
						</td>
						<td>
							${name}&nbsp;
						</td>
						<td>
							|
						  <s:if test="allDepts.size>0"><s:property value="allDepts.size"/>个下级部门|</s:if>
						  <s:if test="allUsers.size>0"><s:property value="allUsers.size"/>个用户|</s:if>
							<s:iterator value="refObjectListMap" id="entry">
								<s:property value="key"/>:<s:property value="value.size" />|
							</s:iterator>
						</td>
						<td>
							<a style="color: #e9621e;text-decoration: none;cursor: pointer;" onclick="edit(${pkId})">修改</a>
							<a style="color: #e9621e;text-decoration: none;cursor: pointer;" onclick="manage(${pkId})">管理</a>
							<a style="color: #e9621e;text-decoration: none;cursor: pointer;" onclick="del(${pkId},'${name}')">删除</a>
						</td>
					</tr>
				</s:iterator>
				<tr class="trStyle" style="<s:if test="page.data.size>0">display: none;</s:if>">
					<td colspan="5">
						无
					</td>
				</tr>
			</table>
			<s:property value="page.pageShowTemplate" escape="false"/>
		</div>
		<s:actionmessage/><s:actionerror/>
		<s:hidden id="deptId" name="deptId"></s:hidden>
		<div style="text-align: center;">
		  <s:if test="page.findByExample.parent.parent!=null">
			<a class="but28" href="javascript:manage(<s:property value="page.findByExample.parent.parent.pkId"/>);" style="text-decoration:none;">返回上级</a> 
		  </s:if>
		</div>
	</div>
	</s:form>
	<script type="text/javascript" src="<%=basePath %>js/jquery.js"></script>
	<script type="text/javascript" src="<%=basePath %>js/jquery.leanModal.min.js"></script>
	<script type="text/javascript" src="<%=basePath %>js/modalWindow.js"></script>
	<script type="text/javascript">
	
		var modalWindow = null;
		
		function add(){
			// 添加组织机构或部门
			var deptId = $("#deptId").val();
			if (deptId == null||deptId=="") {
				deptId=0
			}
			var url = "deptInfo-add.action?item.parId=" + deptId;
			showModalWindow(
					url,
					window,
					'resizable:no;scroll:yes;status:no;dialogWidth=500px;dialogHeight=400px;center=yes;help=no;location=no;',
					processDeptSave
			);
		}
		
		function edit(deptId) {
			// 修改组织机构或部门
			var url = "deptInfo-edit.action?item.pkId=" + deptId;
			showModalWindow(
				url,
				window,
				'resizable:no;scroll:yes;status:no;dialogWidth:500px;dialogHeight:400px;center:yes;help:no;location:no;',
				processDeptSave
			);
		}
		
		function processDeptSave(returnValue){
			if (returnValue != "" && typeof returnValue != "undefined") {
				var result = eval("("+returnValue+")");
				if(result.success){
					if(result.resultType==1||result.resultType==2){
						parent.refreshTree();
						window.location.reload(true);
					}
				}
			}
		}
		
		function userManage(id){
			$("#deptId").val(id);
			$("form")[0].action="../sys/userInfo-manage.action";
			$("form")[0].submit();
		}
		
		function manage(id){
			$("#deptId").val(id);
			$("form")[0].submit();
		}

		function del(id, name) {
			if (confirm("真的要删除["+name+"("+id+")]吗？")) {
				// 删除组织机构或部门
				var action = "deptInfo-del.action";
				var data = {"item.pkId":id};
				$.ajax({
					url: action,
					type: "POST",
					data: data,
					dataType: "json",
					success: function(result){
						if (result.success){
							alert("删除成功！");
							// 刷新组织机构树
							if (parent.refreshTree){
								parent.refreshTree();
							}else{
								alert("未找到刷新方法！");
							}
							$("form")[0].submit();
						}else{
							alert("删除失败："+result.errorMessage);
						}
					}
				});
			}
		}
		
		function addUser() {
			var deptId = $("#deptId").val();
			if (deptId == 0) {
				return false;
			}
			var url = "User-add.action?item.depart.pkId="+deptId;
			showModalWindow(
					url,
					window,
					'resizable:no;scroll:yes;status:no;dialogWidth:640px;dialogHeight:230px;center:yes;help:no'
			);
		}

		function editUser(userId) {
			var url = "User-edit.action?item.pkId=" + userId;
			showModalWindow(
					url,
					window,
					'resizable:no;scroll:yes;status:no;dialogWidth:640px;dialogHeight:230px;center:yes;help:no'
			);
		}
		
		function delteUser(userId, userName) {
			if (confirm("你确定删除用户名叫（" + userName + "）的用户？")) {
				$.ajax({
					url: "User-del.action",
					type: "POST",
					data: {"item.pkId": userId},
					dataType: "json",
					success: function(data){
						if (data.success) {
							alert("用户删除成功！");
							location.reload(true);
						} else {
							alert("用户删除失败：" + data.message);
						}
					}
				});
			}
		}
		
	</script>
</body>
</html>